#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=110;
int heap[maxn],isort[maxn],inp[maxn],wait[maxn],N;
void downadjust(int low,int high){
    int i=low,j=2*i;
    while(j<=high){
        if(j+1<=high && heap[j+1]>heap[j]){
            j=j+1;
        }
        if(heap[j]>heap[i]){
            swap(heap[i],heap[j]);
            i=j;
            j=2*i;
        }
        else {break;}
    }
}
void insertsort(int index){
    sort(isort+1,isort+index+1);
}
bool tell(){
    int i=1;
    while(wait[i]<wait[i+1]){
        i=i+1;
    }
    if(i==1) return true;
    else{return false;}
}
int main(){
    //freopen("in.txt","r",stdin);
    scanf("%d",&N);
    for(int i=1;i<=N;i++){
        scanf("%d",&inp[i]);
    }
    for(int i=1;i<=N;i++){
        scanf("%d",&wait[i]);
    }
    bool flag;
    flag=tell();
    memcpy(heap,wait,sizeof(wait));
    memcpy(isort,wait,sizeof(wait));
    if(flag==false){
        printf("Insertion Sort\n");
        int index=2;
        while(isort[index-1]<=isort[index] && index<=N){
            index++;
        }
        insertsort(index);
        for(int i=1;i<=N;i++){
            if(i==1)printf("%d",isort[i]);
            else printf(" %d",isort[i]);
        }
        printf("\n");
    }
    else{
        printf("Heap Sort\n");
        int index=N;
        while(heap[1]<=heap[index]&&index>2){
            index--;
        }
        swap(heap[1],heap[index]);
        downadjust(1,index-1);
        for(int i=1;i<=N;i++){
            if(i==1)printf("%d",heap[i]);
            else printf(" %d",heap[i]);
        }
    }
}